package nuc.soft.A01.jzs.controller.problem;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nuc.soft.A01.jzs.model.MysqlPageCtrl;
import nuc.soft.A01.jzs.model.general.GeneralUser;
import nuc.soft.A01.jzs.model.problem.SingleChooseProblem;
import nuc.soft.A01.jzs.model.user.Admin;
import nuc.soft.A01.jzs.model.user.Teacher;
import nuc.soft.A01.jzs.services.AdminService;
import nuc.soft.A01.jzs.services.TeacherService;
import nuc.soft.A01.jzs.services.problem.SingleChooseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/teacher/singlechoose")
public class SingleChooseController {


	@Autowired
	private SingleChooseService scs;
	@Autowired
	private TeacherService ts;
	@Autowired
	private AdminService as;
	
	@RequestMapping("/showsearch")
	public String getSearchProblem(HttpServletRequest request,
			HttpServletResponse response,String searchname)
			{
		ArrayList<SingleChooseProblem> singlechooses=scs.getSearchSingleChoose(searchname);
		request.setAttribute("singlechooses", singlechooses);
		return "/problem/singlechoose/index";
			}
	
	@RequestMapping("/showall")
	public String getAllproblem(HttpServletRequest request,
			HttpServletResponse response) {
//		ArrayList<SingleChooseProblem> singlechooses=scs.getAllSingleChoose();
//		request.setAttribute("singlechooses", singlechooses);
//		return "/problem/singlechoose/index";
//		MysqlPageCtrl pagectrl=new MysqlPageCtrl();
//		request.setAttribute("pagectrl", pagectrl);
		return "forward:/teacher/singlechoose/pageshow?pagenum=1";
	}

	@RequestMapping("/pageshow")
	public String getPagerProblem(HttpServletRequest request,
			HttpServletResponse response,MysqlPageCtrl pagectrl)
			{
				pagectrl.setPagesize(5);
				pagectrl=scs.makeCtrl(pagectrl);
				ArrayList<SingleChooseProblem> singlechooses=scs.getCurrentPageSingleChoose(pagectrl);
				request.setAttribute("singlechooses", singlechooses);
				request.setAttribute("pagectrl", pagectrl);
				return "/problem/singlechoose/index";
			}
	
	

	@RequestMapping("/showadd")
	public String showadd(HttpServletRequest request,
			HttpServletResponse response) {
		return "/problem/singlechoose/add";
	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request, HttpServletResponse response,
			SingleChooseProblem problem) {
		HttpSession session=request.getSession();
		GeneralUser gu=(GeneralUser) session.getAttribute("user");
		try
		{
			Teacher teacher=(Teacher)gu;
			problem.setTeacherid(teacher.getTeacherid());
		}
		catch(Exception ex)
		{
			Admin admin=(Admin)gu;
			problem.setTeacherid("admin_"+admin.getAdminid());
		}
		scs.insert(problem);
		return "forward:/teacher/singlechoose/showall";
	}

	@RequestMapping("/showedit")
	public String showedit(HttpServletRequest request,
			HttpServletResponse response, String theid) {
		
		SingleChooseProblem problem=scs.getASingleChooseById(theid);
		request.setAttribute("editproblem", problem);
		return "/problem/singlechoose/edit";
	}

	@RequestMapping("/edit")
	public String edit(HttpServletRequest request,
			HttpServletResponse response, SingleChooseProblem problem) {
		HttpSession session=request.getSession();
		GeneralUser gu=(GeneralUser) session.getAttribute("user");
		try
		{
			Teacher teacher=(Teacher)gu;
			problem.setTeacherid(teacher.getTeacherid());
		}
		catch(Exception ex)
		{
			Admin admin=(Admin)gu;
			problem.setTeacherid("admin_"+admin.getAdminid());
		}
		scs.update(problem);
		return "forward:/teacher/singlechoose/showall";
	}
}
